<template>
  <div class="crops-ranking-bar">
    <div class="bar-list">
      <div class="bar-item" v-for="(item, index) in barData.slice(0, count)" :key="index">
        <div class="bar-wrap">
          <div class="bar-text">
            <div style="display: flex; align-items: center">
              <span class="no" :class="['no' + index]">{{ index + 1 }}</span>
              <span class="text">{{ item.name }}</span>
            </div>
            <span class="value">{{ item.count }}{{ item.unit }}</span>
          </div>
          <div class="bar-bg">
            <div class="bar" :style="{ width: item.rate }"></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getOrgStatistics } from '@/api/h3yun'

export default {
  name: 'OrgCityRateBar',
  props: {
    areaCode: {
      type: String
    },
    title: {
      type: String,
      default: '',
    },
    data: {
      type: Array,
      default() {
        return []
      },
    },
    count: {
      type: Number,
      default: 5
    },
  },
  data() {
    return {
      defaultList: [
        // { name: '五华区', count: 10, unit: '家', rate: '100%' },
        // { name: '盘龙区', count: 9, unit: '家', rate: '90%' },
        // { name: '官渡区', count: 8, unit: '家', rate: '80%' },
        // { name: '西山区', count: 7, unit: '家', rate: '70%' },
        // { name: '呈贡区', count: 6, unit: '家', rate: '60%' },
        // { name: '晋宁区', count: 5, unit: '家', rate: '50%' },
        // { name: '安宁市', count: 4, unit: '家', rate: '40%' },
        // { name: '富民县', count: 3, unit: '家', rate: '30%' },
        // { name: '宜良县', count: 2, unit: '家', rate: '20%' },
        // { name: '石林县', count: 1, unit: '家', rate: '10%' },
      ],
    }
  },
  computed: {
    barData() {
      if (this.data.length === 0) return this.defaultList
      const newData = []
      const len = this.data.slice(0, this.count).length
      let arr = this.data.slice(0, this.count).map(s => s.total)
      let max = Math.max(...arr)
      for (let i = 0; i < len; i++) {
        let item = this.data[i]
        newData.push({ name: item.name, count: item.count, unit: '家', rate: this.getRate(item.count, max) })
      }
      return newData
    }
  },
  watch: {
    areaCode: {
      handler(val) {
        this.initData()
      }
    }
  },
  async mounted() {
    this.initData()
  },
  methods: {
    async initData() {
      let res = await getOrgStatistics(this.areaCode)
      let list = res.data || []
      let countArr = list.map(s => s.count)
      let max = Math.max(...countArr)
      //过滤掉adname为空的数据
      list = list.filter(s => s.adname)
      this.defaultList = list.map(s => ({
        name: s.adname,
        count: s.count,
        unit: '家',
        rate: this.getRate(s.count, max + max / 10)
      }))
    },
    getRate(count, max) {
      //如果count与max相等，则返回100%，否则返回count/max*100%
      return count === max ? '100%' : (count / max) * 100 + '%'
    },
  },
}
</script>

<style lang="scss" scoped>
.crops-ranking-bar {
  width: 100%;
  margin-top: 12px;
  min-height: 80px;

  .bar-list {
    .bar-item {
      display: flex;
      align-items: center;
      margin-bottom: 6px;

      .bar-no {
        flex-basis: 36px;
        flex-shrink: 0;
        width: 32px;
        height: 32px;
        line-height: 36px;
        background: url(@/assets/images/screen/no1-bg.png) no-repeat;
        font-family: PingFangSC-Semibold;
        font-size: 13.33px;
        color: #ffffff;
        letter-spacing: 0;
        text-align: center;
        font-weight: 600;
      }

      .bar-wrap {
        flex: 1;

        .bar-text {
          display: flex;
          justify-content: space-between;
          align-items: flex-end;
          line-height: 16px;

          .no {
            font-family: PingFangSC-Semibold;
            font-size: 13.33px;
            color: #ffffff;
            letter-spacing: 0;
            text-align: center;
            font-weight: 600;
            width: 20px;
            height: 20px;
            line-height: 20px;
            background: rgb(40, 136, 238);
            border-radius: 2px;
          }
          .no0 {
            // background: url(@/assets/images/screen/no1-bg.png) no-repeat;
            // background-size: 100% 100%;
          }
          .no1 {
            // background: url(@/assets/images/screen/no2-bg.png) no-repeat;
            // background-size: 100% 100%;
          }
          .no2 {
            // background: url(@/assets/images/screen/no3-bg.png) no-repeat;
            // background-size: 100% 100%;
          }

          .text {
            margin-left: 8px;
            font-family: PingFangSC-Regular;
            font-size: 14px;
            color: rgba(255, 255, 255, 0.9);
            letter-spacing: 0;
            font-weight: 400;
            padding: 0 6px;
          }

          .value {
            margin-right: 10px;
            font-family: HTYPEtest01-Bold;
            font-size: 14px;
            color: #fff;
            letter-spacing: 0;
            font-weight: 400;
          }
        }

        .bar-bg {
          margin-top: 4px;
          width: 100%;
          background: rgba(179, 235, 255, 0.12);
          height: 8px;

          .bar {
            width: 0;
            height: 8px;
            border-radius: 0 5px 5px 0;
            background-image: linear-gradient(90deg,
                #0761f6 0%,
                #29bab1 51%,
                #f3be09 100%);
          }
        }
      }

      &:last-child {
        margin-bottom: 0;
      }
    }
  }
}
</style>
